﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Sunboxsoft.Common;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using LocalService = Sunboxsoft.ASMS.Client.Domain.LocalService;
using Sunboxsoft.WinForm;
using DevExpress.XtraEditors.Repository;
using System.Windows.Forms;

namespace Sunboxsoft.ASMS.Client.UI
{
    /// <summary>
    /// SearchLookupEdit绑定工具类
    /// </summary>
    public static class SearchLookUpEditUtil
    {
        #region package

        /// <summary>
        /// 绑定套餐列表
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="defaultObjectId"></param>
        /// <param name="canClear"></param>
        public static void BindEPackage(this SearchLookUpEdit lookUpEdit, Promise promise,
            Guid? defaultObjectId,
            bool canClear)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EPackage> result = new Service.QueryService().QueryPackagePagedList(null,
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled, 0, Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;

                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            lookUpEdit.Properties.ShowFooter = false;
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "Name";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            //DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            //colTelephone.Caption = "联系电话";
                            //colTelephone.Name = "Telephone";
                            //colTelephone.VisibleIndex = 1;
                            //colTelephone.FieldName = "Telephone";

                            //DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            //colPinyin.Caption = "拼音";
                            //colPinyin.Name = "Pinyin";
                            //colPinyin.VisibleIndex = 2;
                            //colPinyin.Width = 1;
                            //colPinyin.OptionsColumn.AllowSize = false;
                            //colPinyin.OptionsFilter.AllowFilter = false;
                            //colPinyin.FieldName = "Pinyin";

                            //DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            //colLetter.Caption = "字母";
                            //colLetter.Name = "Letter";
                            //colLetter.VisibleIndex = 3;
                            //colLetter.Width = 1;
                            //colLetter.OptionsColumn.AllowSize = false;
                            //colLetter.OptionsFilter.AllowFilter = false;
                            //colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName });//, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";

                        if (defaultObjectId != null)
                        {
                            for (int index = 0; index < result.Data.Items.Count; index++)
                            {
                                if (result.Data.Items[index].Id == defaultObjectId)
                                {
                                    lookUpEdit.EditValue = result.Data.Items[index];
                                    break;
                                }
                            }
                        }

                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }
        #endregion

        /// <summary>
        /// 绑定供应商
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="showFirstObject"></param>
        /// <param name="canClear"></param>
        public static void BindVSupplier(this SearchLookUpEdit lookUpEdit, Promise promise,
            bool showFirstObject = true,
            bool canClear = true)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VSupplier> result = new Service.QueryService().QuerySupplierPagedList(
                    "",
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    Domain.Constant.CommonStatus.Enabled,
                    0,
                    Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowFooter = false;
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowFooter = false;
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "Name";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "电话";
                            colTelephone.Name = "Telephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "Telephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "Pinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "Letter";
                            colLetter.VisibleIndex = 3;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";
                        if (showFirstObject && result.Data.Items.Count > 0)
                        {
                            lookUpEdit.EditValue = result.Data.Items.First();
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 绑定员工WebRef.EStaff
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="staffPost"></param>
        /// <param name="showFirstObject"></param>
        /// <param name="canClear"></param>
        public static void BindEStaff(this SearchLookUpEdit lookUpEdit, Promise promise,
            bool showFirstObject = true,
            bool canClear = true)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EStaff> result = new Service.QueryService().QueryStaffPagedList(
                    null,
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    Domain.Constant.CommonStatus.Enabled,
                    0,
                    Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowFooter = false;
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowFooter = false;
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "Name";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "联系电话";
                            colTelephone.Name = "Telephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "Telephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "Pinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.Width = 0;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "Letter";
                            colLetter.VisibleIndex = 3;
                            colLetter.Width = 0;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";
                        if (showFirstObject && result.Data.Items.Count > 0)
                        {
                            lookUpEdit.EditValue = result.Data.Items.First();
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="defaultObjectId"></param>
        /// <param name="canClear"></param>
        public static void BindEStaff(this SearchLookUpEdit lookUpEdit, Promise promise,
            Guid? defaultObjectId,
            bool canClear)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EStaff> result = new Service.QueryService().QueryStaffPagedList(
                    null,
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId.Value,
                    Domain.Constant.CommonStatus.Enabled,
                    0,
                    Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;

                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Clear();
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "Name";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "联系电话";
                            colTelephone.Name = "Telephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "Telephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "Pinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "Letter";
                            colLetter.VisibleIndex = 3;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";

                        if (defaultObjectId != null)
                        {
                            for (int index = 0; index < result.Data.Items.Count; index++)
                            {
                                if (result.Data.Items[index].Id == defaultObjectId)
                                {
                                    lookUpEdit.EditValue = result.Data.Items[index];
                                    break;
                                }
                            }
                        }

                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 设置lookUP选中的项目
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="selectedObjectId"></param>
        public static void SelectEStaff(this SearchLookUpEdit lookUpEdit, Guid selectedObjectId)
        {
            List<WebRef.EStaff> dataSource = lookUpEdit.Properties.DataSource as List<WebRef.EStaff>;
            for (int index = 0; index < dataSource.Count; index++)
            {
                if (dataSource[index].Id == selectedObjectId)
                {
                    lookUpEdit.EditValue = dataSource[index];
                    break;
                }
            }
        }

        #region VStaffPosition

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="post"></param>
        /// <param name="selectFirstObject"></param>
        /// <param name="canClear"></param>
        public static void BindVStaffPosition(this SearchLookUpEdit lookUpEdit, Promise promise,
            string post,
            bool selectFirstObject = true,
            bool canClear = true)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.VStaffPosition> result = new Service.QueryService().QueryStaffPositionList(
                    null,
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId,
                    post);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;

                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            lookUpEdit.Properties.ShowFooter = false;
                            lookUpEdit.Properties.PopupFormSize = new System.Drawing.Size(280, 400);
                            lookUpEdit.Properties.PopupResizeMode = ResizeMode.LiveResize;
                            lookUpEdit.Properties.ShowFooter = false;
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "StaffName";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "StaffName";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "联系电话";
                            colTelephone.Name = "StaffTelephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "StaffTelephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "StaffPinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.Width = 1;
                            colPinyin.MaxWidth = 1;
                            colPinyin.OptionsColumn.AllowMove = false;
                            colPinyin.OptionsColumn.AllowSize = false;
                            colPinyin.FieldName = "StaffPinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "StaffLetter";
                            colLetter.VisibleIndex = 3;
                            colLetter.Width = 1;
                            colLetter.MaxWidth = 1;
                            colLetter.OptionsColumn.AllowMove = false;
                            colLetter.OptionsColumn.AllowSize = false;
                            colLetter.FieldName = "StaffLetter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "StaffName";
                        if (selectFirstObject && result.Data.Items.Count > 0)
                        {
                            lookUpEdit.EditValue = result.Data.Items.First();
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="post">岗位</param>
        /// <param name="selectedStaffId">选中的员工Id</param>
        /// <param name="canClear"></param>
        public static void BindVStaffPosition(this SearchLookUpEdit lookUpEdit, Promise promise,
            string post,
            Guid? selectedStaffId,
            bool canClear)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.VStaffPosition> result = new Service.QueryService().QueryStaffPositionList(
                    null,
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId,
                    post);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;

                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            lookUpEdit.Properties.ShowFooter = false;
                            lookUpEdit.Properties.PopupFormSize = new System.Drawing.Size(280, 400);
                            lookUpEdit.Properties.PopupResizeMode = ResizeMode.LiveResize;
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Clear();
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "StaffName";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "StaffName";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "联系电话";
                            colTelephone.Name = "StaffTelephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "StaffTelephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "StaffPinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.Width = 1;
                            colPinyin.MaxWidth = 1;
                            colPinyin.OptionsColumn.AllowMove = false;
                            colPinyin.OptionsColumn.AllowSize = false;
                            colPinyin.FieldName = "StaffPinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "StaffLetter";
                            colLetter.VisibleIndex = 3;
                            colLetter.Width = 1;
                            colLetter.MaxWidth = 1;
                            colLetter.OptionsColumn.AllowMove = false;
                            colLetter.OptionsColumn.AllowSize = false;
                            colLetter.FieldName = "StaffLetter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "StaffName";

                        if (selectedStaffId != null)
                        {
                            for (int index = 0; index < result.Data.Items.Count; index++)
                            {
                                if (result.Data.Items[index].StaffId == selectedStaffId)
                                {
                                    lookUpEdit.EditValue = result.Data.Items[index];
                                    break;
                                }
                            }
                        }

                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 设置lookUP选中的项目
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="selectedObjectId"></param>
        public static void SelectVStaffPosition(this SearchLookUpEdit lookUpEdit, Guid selectedStaffId)
        {
            List<WebRef.VStaffPosition> dataSource = lookUpEdit.Properties.DataSource as List<WebRef.VStaffPosition>;
            for (int index = 0; index < dataSource.Count; index++)
            {
                if (dataSource[index].StaffId == selectedStaffId)
                {
                    lookUpEdit.EditValue = dataSource[index];
                    break;
                }
            }
        }
        #endregion


        /// <summary>
        /// 绑定仓库
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="selectedWarehouseId"></param>
        /// <param name="canClear"></param>
        public static void BindEWarehouse(this SearchLookUpEdit lookUpEdit, Promise promise,
            Guid? selectedWarehouseId,
            bool canClear)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VWarehouse> result = new Service.QueryService().QueryWarehousePagedList(
                    "",
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled,
                    0,
                    Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;

                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Clear();
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "Name";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "Pinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "Letter";
                            colLetter.VisibleIndex = 3;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colPinyin, colLetter });
                        }

                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";

                        if (selectedWarehouseId != null)
                        {
                            for (int index = 0; index < result.Data.Items.Count; index++)
                            {
                                if (result.Data.Items[index].Id == selectedWarehouseId)
                                {
                                    lookUpEdit.EditValue = result.Data.Items[index];
                                    break;
                                }
                            }
                        }

                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 设置lookUP选中的项目
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="selectedObjectId"></param>
        public static void SelectEWarehouse(this SearchLookUpEdit lookUpEdit, Guid warehouseId)
        {
            List<WebRef.VWarehouse> dataSource = lookUpEdit.Properties.DataSource as List<WebRef.VWarehouse>;
            for (int index = 0; index < dataSource.Count; index++)
            {
                if (dataSource[index].Id == warehouseId)
                {
                    lookUpEdit.EditValue = dataSource[index];
                    break;
                }
            }
        }


        /// <summary>
        /// 绑定充值类型WebRef.ERechargeItem
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="showFirstObject"></param>
        /// <param name="canClear"></param>
        public static void BindERechargeItem(this SearchLookUpEdit lookUpEdit, Promise promise,
            bool showFirstObject = true,
            bool canClear = true)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.ERechargeItem> result = new Service.QueryService().QueryRechargeItemPagedList(ModuleContext.Current.BusinessId,
                  ModuleContext.Current.StationId,
                  Domain.Constant.CommonStatus.Enabled, 0, Domain.Constant.PageLimitAll);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Clear();
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colCode = new DevExpress.XtraGrid.Columns.GridColumn();
                            colCode.Caption = "编码";
                            colCode.VisibleIndex = 0;
                            colCode.FieldName = "Code";

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.VisibleIndex = 1;
                            colName.FieldName = "Name";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colCode, colName });
                        }
                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";
                        if (showFirstObject && result.Data.Items.Count > 0)
                        {
                            lookUpEdit.EditValue = result.Data.Items.First();
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 绑定主数据WebRef.EMasterDataItem
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="masterDataCode"></param>
        /// <param name="parentCode"></param>
        /// <param name="tag"></param>
        /// <param name="showFirstObject">是否显示第一个对象</param>
        /// <param name="canClear">是否显示清除按钮</param>
        public static void BindEMasterDataItem(this SearchLookUpEdit lookUpEdit, Promise promise,
            string masterDataCode,
            string parentCode = null,
            int? tag = null,
            bool showFirstObject = true,
            bool canClear = true)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.EMasterDataItem> result = new LocalService.QueryService().QueryMasterDataItemList(masterDataCode, parentCode, tag);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            lookUpEdit.Properties.ShowFooter = false;
                            lookUpEdit.Properties.PopupFormSize = new System.Drawing.Size(150, 400);
                            lookUpEdit.Properties.PopupResizeMode = ResizeMode.LiveResize;
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.VisibleIndex = 1;
                            colPinyin.Width = 1;
                            colPinyin.MaxWidth = 1;
                            colPinyin.OptionsColumn.AllowMove = false;
                            colPinyin.OptionsColumn.AllowSize = false;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.VisibleIndex = 2;
                            colLetter.Width = 1;
                            colLetter.MaxWidth = 1;
                            colLetter.OptionsColumn.AllowMove = false;
                            colLetter.OptionsColumn.AllowSize = false;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colPinyin, colLetter });
                        }
                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";

                        if (showFirstObject && result.Data.Items.Count > 0)
                        {
                            lookUpEdit.EditValue = result.Data.Items.First();
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 绑定主数据WebRef.EMasterDataItem
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="promise"></param>
        /// <param name="masterDataCode"></param>
        /// <param name="parentCode"></param>
        /// <param name="tag"></param>
        /// <param name="defaultObjectId"></param>
        /// <param name="canClear">是否显示清除按钮</param>
        public static void BindEMasterDataItem(this SearchLookUpEdit lookUpEdit, Promise promise,
            string masterDataCode,
            string parentCode,
            int? tag,
            string defaultObjectId,
            bool canClear)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.EMasterDataItem> result = new LocalService.QueryService().QueryMasterDataItemList(masterDataCode, parentCode, tag);
                lookUpEdit.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.Properties.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            lookUpEdit.Properties.ShowFooter = false;
                            lookUpEdit.Properties.PopupFormSize = new System.Drawing.Size(150, 400);
                            lookUpEdit.Properties.PopupResizeMode = ResizeMode.LiveResize;
                            if (canClear)
                            {
                                lookUpEdit.Properties.ShowClearButton = true;
                                lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                                lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                            }
                            else
                            {
                                lookUpEdit.Properties.ShowClearButton = false;
                            }

                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "Name";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.VisibleIndex = 1;
                            colPinyin.Width = 1;
                            colPinyin.MaxWidth = 1;
                            colPinyin.OptionsColumn.AllowMove = false;
                            colPinyin.OptionsColumn.AllowSize = false;
                            colPinyin.FieldName = "Pinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.VisibleIndex = 2;
                            colLetter.Width = 1;
                            colLetter.MaxWidth = 1;
                            colLetter.OptionsColumn.AllowMove = false;
                            colLetter.OptionsColumn.AllowSize = false;
                            colLetter.FieldName = "Letter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colPinyin, colLetter });
                        }
                        lookUpEdit.Properties.DataSource = result.Data.Items;
                        lookUpEdit.Properties.DisplayMember = "Name";

                        if (string.IsNullOrEmpty(defaultObjectId) && result.Data.Items.Count > 0)
                        {
                            for (int index = 0; index < result.Data.Items.Count; index++)
                            {
                                if (string.Equals(result.Data.Items[index].Code, defaultObjectId))
                                {
                                    lookUpEdit.EditValue = result.Data.Items[index];
                                    break;
                                }
                            }
                        }
                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        /// <summary>
        /// 设置lookUP选中的项目
        /// </summary>
        /// <param name="lookUpEdit"></param>
        /// <param name="selectedObjectId"></param>
        public static void SelectEMasterDataItem(this SearchLookUpEdit lookUpEdit, string selectedObjectId)
        {
            List<WebRef.EMasterDataItem> dataSource = lookUpEdit.Properties.DataSource as List<WebRef.EMasterDataItem>;
            for (int index = 0; index < dataSource.Count; index++)
            {
                if (string.Equals(dataSource[index].Code, selectedObjectId))
                {
                    lookUpEdit.EditValue = dataSource[index];
                    break;
                }
            }
        }

        public static void BindIntCodeTextItem(this SearchLookUpEdit lookUpEdit, Domain.IntCodeTextItem[] items,
            Domain.IntCodeTextItem defaultItem = null,
            bool canClear = false)
        {
            lookUpEdit.Properties.NullText = string.Empty;
            DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.Properties.View as DevExpress.XtraGrid.Views.Grid.GridView;
            if (gridView.Columns.Count == 0)
            {
                lookUpEdit.Properties.ShowFooter = false;
                if (canClear)
                {
                    lookUpEdit.Properties.ShowClearButton = true;
                    lookUpEdit.Properties.Buttons.Add(new EditorButton("clear", ButtonPredefines.Close));
                    lookUpEdit.Properties.ButtonClick += new ButtonPressedEventHandler(OnLookUpEditButtonClick);
                }
                else
                {
                    lookUpEdit.Properties.ShowClearButton = false;
                }

                DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                colName.Width = 120;
                colName.VisibleIndex = 0;
                colName.Caption = "名称";
                colName.FieldName = "Text";

                gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName });

            }

            lookUpEdit.Properties.DataSource = items;
            lookUpEdit.Properties.DisplayMember = "Text";
            if (defaultItem != null)
            {
                lookUpEdit.EditValue = defaultItem;
            }
        }

        public static void BindVStaffPosition(this RepositoryItemSearchLookUpEdit lookUpEdit, Control control, Promise promise,
            string post)
        {
            UIContext.Current.InvokeAsync(() =>
            {
                ListResult<WebRef.VStaffPosition> result = new Service.QueryService().QueryStaffPositionList(
                    null,
                    ModuleContext.Current.BusinessId.Value,
                    ModuleContext.Current.StationId,
                    post);
                control.BeginInvoke(new Action(() =>
                {
                    lookUpEdit.NullText = string.Empty;
                    if (result.IsSuccess)
                    {
                        DevExpress.XtraGrid.Views.Grid.GridView gridView = lookUpEdit.View as DevExpress.XtraGrid.Views.Grid.GridView;
                        if (gridView.Columns.Count == 0)
                        {
                            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
                            colName.Caption = "名称";
                            colName.Name = "StaffName";
                            colName.VisibleIndex = 0;
                            colName.FieldName = "StaffName";

                            DevExpress.XtraGrid.Columns.GridColumn colTelephone = new DevExpress.XtraGrid.Columns.GridColumn();
                            colTelephone.Caption = "联系电话";
                            colTelephone.Name = "StaffTelephone";
                            colTelephone.VisibleIndex = 1;
                            colTelephone.FieldName = "StaffTelephone";

                            DevExpress.XtraGrid.Columns.GridColumn colPinyin = new DevExpress.XtraGrid.Columns.GridColumn();
                            colPinyin.Caption = "拼音";
                            colPinyin.Name = "StaffPinyin";
                            colPinyin.VisibleIndex = 2;
                            colPinyin.FieldName = "StaffPinyin";

                            DevExpress.XtraGrid.Columns.GridColumn colLetter = new DevExpress.XtraGrid.Columns.GridColumn();
                            colLetter.Caption = "字母";
                            colLetter.Name = "StaffLetter";
                            colLetter.VisibleIndex = 3;
                            colLetter.FieldName = "StaffLetter";

                            gridView.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName, colTelephone, colPinyin, colLetter });
                        }

                        lookUpEdit.DataSource = result.Data.Items;
                        lookUpEdit.DisplayMember = "StaffName";
                        lookUpEdit.ValueMember = "StaffId";

                        promise.Resolve();
                    }
                    else
                    {
                        promise.Reject();
                    }
                }));
            });
        }

        static void OnLookUpEditButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Tag != null && string.Equals("clear", e.Button.Tag.ToString()))
            {
                SearchLookUpEdit lookUpEdit = sender as SearchLookUpEdit;
                if (lookUpEdit != null)
                {
                    lookUpEdit.EditValue = null;
                }
            }
        }

        /// <summary>
        /// 选取选择的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lookUpEdit"></param>
        /// <returns></returns>
        public static T GetSelectedValue<T>(this SearchLookUpEdit lookUpEdit) where T : class
        {
            T selectedValue = lookUpEdit.EditValue as T;
            return selectedValue;
        }
    }
}
